/*
  Copyright (c) 2009, Pablo Davi Faria da Silva
  Novesfora.com.br
  Blog.des84.com
  All rights reserved.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
  
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the 
    documentation and/or other materials provided with the distribution.
  
  * Neither the name of Novesfora, DES84 nor the names of its 
    contributors may be used to endorse or promote products derived from 
    this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package Born.video
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.media.SoundTransform;
	import flash.text.TextField;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import com.greensock.TweenLite;
	
	import Born.events.Events;
	import Born.loader.LoadFile;
	import Born.utils.Date.ConfigureHour;

	public class VideoPlayerFull extends VideoPlayer
	{
		private var $play				:MovieClip;
		private var $seek				:MovieClip;
		private var $seekMax			:Number;
		private var $seekPercent		:Number;
		private var $image				:String;
		private var $paused				:Boolean;
		private var $firstRun			:Boolean;
		private var $viLoader			:LoadFile;
		private var $soundTransform		:SoundTransform;
		private var $volume				:Number;
		private var $timeNow			:String;
		private var $timeTotal			:String;
		private var $timeNowText		:TextField;
		private var $timeTotalText		:TextField;
		private var $nowTime			:uint;
		private var $totalTime			:uint;
		
		public function VideoPlayerFull() 
		{
			$firstRun = true;
			this.addEventListener(Event.REMOVED_FROM_STAGE, removeAll);
		}
		/**
		 * Set SeekBar of the video Control 
		 * @param _seek
		 */		
		public function set VideoSeek(_seek:MovieClip):void
		{
			$seek = _seek;
			$seekMax = $seek.width;
			$seek.addEventListener(Event.ENTER_FRAME, videoControl);
		}
		/**
		 * Execute Seek move and grow 
		 * @param e
		 */		
		private function videoControl(e:Event):void
		{
			$seekPercent = Math.floor((netStream.time/videoDuration) * 100);
			if ( $seekPercent > 100 ) $seekPercent = 100;
			TweenLite.to($seek, 0.5, { width : ($seekMax/100)*$seekPercent });
		}
		/**
		 * Set the image that will be displayed<br />
		 * before play the video 
		 * @param _image :String
		 */		
		public function set VideoImage(_image:String):void
		{
			$image = _image;
			
			$viLoader = new LoadFile($image);
			$viLoader.alpha = 0;
			$viLoader.addEventListener(Events.IOError, viLoaderError);
			$viLoader.addEventListener(Events.COMPLETE, viLoaderComplete);
		}
		/**
		 * Error on load VideoImage 
		 * @param e
		 */		
		private function viLoaderError(e:IOErrorEvent):void
		{
			trace("Error onload VideoImage");
		}
		/**
		 * Complete and show VideoImage 
		 * @param e
		 */		
		private function viLoaderComplete(e:Event):void
		{
			this.addChild($viLoader);
			TweenLite.to($viLoader, 0.5, { alpha : 1 });
		}
		/**
		 * Remove VideoImage on load
		 */		
		private function viLoaderRemove():void
		{
			try { this.removeChild($viLoader); }
			catch ( e:Error ) {}
			$firstRun = false;
		}
		/**
		 * Set the Play/Pause MovieClip 
		 * @param _playPuse
		 */		
		public function set PlayPauseButtom(_playPuse:MovieClip):void
		{
			$play = _playPuse;
			$play.addEventListener(MouseEvent.CLICK, PlayVideoPlayerFull);
		}
		/**
		 * Will play and pause the video 
		 * @param e :MouseEvent
		 */		
		private function PlayVideoPlayerFull(e:MouseEvent):void
		{
			if ( $firstRun ) TweenLite.to($viLoader, 0.5, { alpha : 0, onComplete : viLoaderRemove });
			
			if ( videoRun == null || videoRun == "NetStream.Play.Stop" ) 
			{
				netStream.play(videoURL);
				$paused = false;
			}
			if ( videoRun == "NetStream.Buffer.Full" )
			{
				if ( !$paused )
				{
					netStream.pause();
					$paused = true;
				}
				else
				{
					netStream.resume();
					$paused = false;
				}
			}
		}
		/**
		 * Receive the number of the VideoVolume<br />
		 * Between 0 and 1
		 * @param _volume :Number
		 */		
		public function set VideoVolume(_volume:Number):void
		{
			if ( _volume > 1 ) _volume = 1;
			if ( _volume < 0 ) _volume = 0;
			$volume = _volume;
			$soundTransform = new SoundTransform(_volume);
			netStream.soundTransform = $soundTransform;
		}
		/**
		 * Get the video Volume 
		 * @return 
		 */		
		public function get VideoVolume():Number
		{
			return $volume;
		}
		/**
		 * Set the TextField for timeNow 
		 * @param _timeNow :TextField
		 */		
		public function set VideoTimeNow(_timeNow:TextField):void
		{
			$timeNowText = _timeNow;
			$nowTime = setInterval(function():void 
				{
					$timeNowText.text = ConfigureHour.simpleHour(Math.ceil(netStream.time));
				}
			, 1000);
		}
		/**
		 * Set the TextField for timeTotal 
		 * @param _timeNow :TextField
		 */	
		public function set VideoTimeTotal(_timeTotal:TextField):void
		{
			$timeTotalText = _timeTotal;
			$totalTime = setInterval(function():void
				{
					$timeTotalText.text = ConfigureHour.simpleHour(videoDuration);
				}
			, 1000);
		}
		/**
		 * Calculate the time Now
		 * @return String
		 */		
		private function VideoTimeNowString():String
		{
			$timeNow = String(netStream.time);
			return $timeNow;
		}
		/**
		 * Remove interval, listener and others 
		 * @param e
		 */		
		private function removeAll(e:Event):void
		{
			clearInterval($nowTime);
			try { $seek.removeEventListener(Event.ENTER_FRAME, videoControl); }
			catch (e:Error) {}
		}
		
	} // End Class
	
} // End Package